package com.gec.oasys.controller;

import com.alibaba.fastjson.JSONArray;
import com.gec.oasys.pojo.SysMenu;
import com.gec.oasys.pojo.SysRole;
import com.gec.oasys.service.SysMenuService;
import com.gec.oasys.service.SysRoleService;
import com.gec.oasys.util.SoulPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysMenuService sysMenuService;
    @RequestMapping("/findSysRoleById")
    @ResponseBody
    public Map<String, Object> findSysRoleById(Integer id){
        HashMap<String, Object> mapData = new HashMap<>();
        String code="1",msg="内部错误";
        SysRole sysRole=sysRoleService.selectByPrimaryKey(id);
        code="0";msg="成功";
        mapData.put("code",code);
        mapData.put("msg",msg);
        mapData.put("data",sysRole);
        return mapData;
    }
    @RequestMapping("/toSysRoleList")
    public String toSysRoleList(){
        return "sysrole/sysrolelist";
    }
    @RequestMapping("/deleteSysRoleById")
    @ResponseBody
    public Map<String,Object> deleteSysRoleById(Integer id){
        HashMap<String, Object> mapData = new HashMap<>();
        String code="1",msg="内部错误";
        int pos =sysRoleService.deleteByPrimaryKey(id);
        if(pos>0){
            code="0";msg="成功";
        }
        mapData.put("msg",msg);
        mapData.put("code",code);
        return mapData;
    }
    @RequestMapping("/deleteSysRolesById")
    @ResponseBody
    public Map<String,Object> deleteSysRolesById(String ids){
        HashMap<String, Object> mapData = new HashMap<>();
        String code="1",msg="内部错误";
        String strids[]=ids.split(",");
        int pos =sysRoleService.deletesByPrimaryKey(strids);
        if(pos>0){
            code="0";msg="成功";
        }
        mapData.put("msg",msg);
        mapData.put("code",code);
        return mapData;
    }
    @RequestMapping("/toSysRoleAdd")
    public String toSysRoleAdd(){
        return "sysrole/sysroleadd";
    }
    @RequestMapping("/saveSysRole")
    @ResponseBody
    public Map<String,Object> saveSysRole(SysRole sysRole){
        Map<String,Object> mapData=new HashMap<>();
        String code="1",msg="内部错误";
        int pos=sysRoleService.insert(sysRole);
        if(pos>0){
            code="0";msg="成功";
        }
        mapData.put("msg",msg);
        mapData.put("code",code);
        return mapData;
    }
    @RequestMapping("/updateBySysRolePrimaryKey")
    @ResponseBody
    public Map<String,Object> updateByPrimaryKey(SysRole sysRole){
        Map<String,Object> mapData=new HashMap<>();
        String code="1",msg="内部错误";
        int pos=sysRoleService.updateByPrimaryKey(sysRole);
        if(pos>0){
            code="0";msg="成功";
        }
        mapData.put("msg",msg);
        mapData.put("code",code);
        return mapData;
    }
    @RequestMapping("/getDataBySysRole")
    @ResponseBody
    public Object dataGrid(SoulPage<SysRole> soulPage, SysRole sysRole) {
        soulPage.setObj(sysRole);
        return sysRoleService.dataGrid(soulPage);
    }
    @RequestMapping("/savePermission")
    @ResponseBody
    public Map<String,Object> savePermission(Integer roleId,@RequestBody String checkedMenuTreeDataJson) {
        Map<String,Object> mapData=new HashMap<>();
        String code="1",msg="内部错误";
        try {
            sysMenuService.addCheckedMenuTreeData(checkedMenuTreeDataJson,roleId);
            code="0";msg="成功";
        }catch (Exception e) {
            e.printStackTrace();
        }
        mapData.put("code",code);
        mapData.put("msg",msg);
        return mapData;
    }
    @RequestMapping("/sysRoleDisuseable")
    @ResponseBody
    public Map<String,Object> sysRoleDisuseable(SysRole sysRole){
        Map<String,Object> mapData=new HashMap<>();
        String code="1",msg="内部错误";
        sysRole.setUseable("1");
        sysRoleService.updateByPrimaryKey(sysRole);
        code="0";msg="成功";
        mapData.put("code",code);
        mapData.put("msg",msg);
        return mapData;
    }
    @RequestMapping("/sysRoleUseable")
    @ResponseBody
    public Map<String,Object> sysRoleUseable(SysRole sysRole){
        Map<String,Object> mapData=new HashMap<>();
        String code="1",msg="内部错误";
        sysRole.setUseable("0");
        sysRoleService.updateByPrimaryKey(sysRole);
        code="0";msg="成功";
        mapData.put("code",code);
        mapData.put("msg",msg);
        return mapData;
    }
}
